package vue;

import java.awt.Adjustable;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;

import controleur.ControleurToile;

import modele.ModeleToile;


public class VueFenetre extends JFrame implements Runnable {
	
	private static final long serialVersionUID = 1L;
	private JScrollPane jsp;
	private JButton undo;
	private JButton redo;
	private JButton copier;
	private JButton couper;
	private JButton coller;
	private JButton sauvegarder;
	private JButton generer;
	private JMenuItem save;
	private JMenuItem cancel;
	private JMenuItem back;
	private JMenuItem copy;
	private JMenuItem cut;
	private JMenuItem paste;
	private JMenuItem generate;
	private JMenuItem targetFolder;
	private JMenu lastFiles;
	private ControleurToile controleur;

	public void run() {
		
		this.setTitle("KCK UML");
	    this.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
	    
	    /* La Toile sur laquelle on va dessiner */
		ModeleToile modeleToile = new ModeleToile();
		VueToile toile = new VueToile(modeleToile);
		//toile.setPreferredSize(new Dimension(1000, 600));
		
		/* Barre d'outils */
		this.controleur = new ControleurToile(toile, this);
		
		JToolBar outils = new JToolBar();
		
		JButton classe = new JButton(new ImageIcon("Images/newclasse.gif"));
		classe.setActionCommand("NewClasse");
		classe.addActionListener(controleur);
		
		JButton association = new JButton(new ImageIcon("Images/association2.gif"));
		association.setActionCommand("NewAssociation");
		association.addActionListener(controleur);
		
		JButton composition = new JButton(new ImageIcon("Images/composition2.gif"));
		composition.setActionCommand("NewComposition");
		composition.addActionListener(controleur);
		
		JButton agregation = new JButton(new ImageIcon("Images/agregation2.gif"));
		agregation.setActionCommand("NewAgregation");
		agregation.addActionListener(controleur);
		
		undo = new JButton(new ImageIcon("Images/undo.png"));
		undo.setToolTipText("Annuler");
		undo.setActionCommand("AnnulerOperation");
		undo.addActionListener(controleur);
		
		redo = new JButton(new ImageIcon("Images/redo.png"));
		redo.setToolTipText("Rétablir");
		redo.setActionCommand("RefaireOperation");
		redo.addActionListener(controleur);
		
		copier = new JButton(new ImageIcon("Images/copy.png"));
		copier.setToolTipText("Copier");
		copier.setActionCommand("CopierClasse");
		copier.addActionListener(controleur);
		
		couper = new JButton(new ImageIcon("Images/cut.png"));
		couper.setToolTipText("Couper");
		couper.setActionCommand("CouperClasse");
		couper.addActionListener(controleur);
		
		coller = new JButton(new ImageIcon("Images/paste.png"));
		coller.setToolTipText("Coller");
		coller.setActionCommand("CollerClasse");
		coller.addActionListener(controleur);
		
		JButton sauvegarderSous = new JButton(new ImageIcon("Images/sauvegarder_sous.png"));
		sauvegarderSous.setToolTipText("Enregistrer sous ...");
		sauvegarderSous.setActionCommand("Enregistrer sous ...");
		sauvegarderSous.addActionListener(controleur);
		
		sauvegarder = new JButton(new ImageIcon("Images/sauvegarder.png"));
		sauvegarder.setToolTipText("Enregistrer");
		sauvegarder.setActionCommand("Enregistrer");
		sauvegarder.addActionListener(controleur);
		
		generer = new JButton(new ImageIcon("Images/generer-code.png"));
		generer.setToolTipText("Génerer le code");
		generer.setActionCommand("GenererCode");
		generer.addActionListener(controleur);
		
		outils.add(sauvegarderSous);
		outils.add(sauvegarder);
		outils.addSeparator();
		outils.add(classe);
		outils.add(association);
		outils.add(composition);
		outils.add(agregation);
		outils.addSeparator();
		outils.add(undo);
		outils.add(redo);
		outils.addSeparator();
		outils.add(copier);
		outils.add(couper);
		outils.add(coller);
		outils.addSeparator();
		outils.add(generer);
		
		
		/* Barre des menus */
		JMenuBar jmb = new JMenuBar();

		JMenu file = new JMenu("Fichier");
		file.setMnemonic(KeyEvent.VK_F);
		
		JMenuItem neww = new JMenuItem("Nouveau", new ImageIcon("Images/nouveau.png")); 
		neww.setActionCommand("NouveauFichier");
		neww.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, Event.CTRL_MASK));
		neww.addActionListener(controleur); 
		file.add(neww);
		
		JMenuItem open = new JMenuItem("Ouvrir", new ImageIcon("Images/ouvrir.png"));
		open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, Event.CTRL_MASK));
		open.addActionListener(controleur); 
		file.add(open);
		file.addSeparator();
		
		save = new JMenuItem("Enregistrer", new ImageIcon("Images/sauvegarder.png"));
		save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, Event.CTRL_MASK));
		save.addActionListener(controleur);
		file.add(save);
		
		JMenuItem save_as = new JMenuItem("Enregistrer sous ...", new ImageIcon("Images/sauvegarder_sous.png"));
		save_as.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_U, Event.CTRL_MASK));
		save_as.addActionListener(controleur);
		file.add(save_as);
		file.addSeparator();
		
		JMenuItem exit = new JMenuItem("Quitter", new ImageIcon("Images/exit-32.png"));  
		exit.setActionCommand("Quitter");
		exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, Event.CTRL_MASK));
		exit.addActionListener(controleur);
		file.add(exit);
		file.addSeparator();

		this.lastFiles = new JMenu("Derniers fichiers ouverts");
		JMenuItem vide = new JMenuItem("     (Vide)     ");
		vide.setEnabled(false);
		this.lastFiles.add(vide);
		file.add(lastFiles);
		
		JMenu edit = new JMenu("Edition");
		edit.setMnemonic(KeyEvent.VK_E);
		
		cancel = new JMenuItem("Annuler", new ImageIcon("Images/undo.png"));
		cancel.setActionCommand("AnnulerOperation");
		cancel.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z, Event.CTRL_MASK));
		cancel.addActionListener(controleur);
		edit.add(cancel);
		
		back = new JMenuItem("Rétablir", new ImageIcon("Images/redo.png"));
		back.setActionCommand("RefaireOperation");
		back.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Y, Event.CTRL_MASK));
		back.addActionListener(controleur);
		edit.add(back);
		edit.addSeparator();
		
		copy = new JMenuItem("Copier", new ImageIcon("Images/copy.png"));
		copy.setActionCommand("CopierClasse");
		copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, Event.CTRL_MASK));
		copy.addActionListener(controleur);
		edit.add(copy);
		
		cut = new JMenuItem("Couper", new ImageIcon("Images/cut.png"));
		cut.setActionCommand("CouperClasse");
		cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, Event.CTRL_MASK));
		cut.addActionListener(controleur);
		edit.add(cut);
		
		paste = new JMenuItem("Coller", new ImageIcon("Images/paste.png"));
		paste.setActionCommand("CollerClasse");
		paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, Event.CTRL_MASK));
		paste.addActionListener(controleur);
		edit.add(paste);
		
		JMenu source = new JMenu("Source");
		source.setMnemonic(KeyEvent.VK_S);
		
		generate = new JMenuItem("Génerer le code", new ImageIcon("Images/generer-code.png"));
		generate.setActionCommand("GenererCode");
		generate.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G, Event.CTRL_MASK));
		generate.addActionListener(controleur);
		source.add(generate);
		
		targetFolder = new JMenuItem("Dossier destination", new ImageIcon("Images/generer-code.png"));
		targetFolder.setActionCommand("DossierDest");
		targetFolder.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D, Event.CTRL_MASK));
		targetFolder.addActionListener(controleur);
		source.add(targetFolder);
		
		jmb.add(file);
		jmb.add(edit);
		jmb.add(source);
		
		setUndoBtnEnabled(false);
		setRedoBtnEnabled(false);
		setSauvegarderBtnEnabled(false);
		setCopyAndCutBtnEnabled(false);
		setPasteBtnEnabled(false);
		setGenerateBtnEnabled(false);
		
		
		/* Réglages de la fenêtre */
		this.setLayout(new BorderLayout());
		
		jsp = new JScrollPane(toile);
		jsp.setPreferredSize(new Dimension(1000, 600));
		ScrollListener scrolling = new ScrollListener();
		jsp.getHorizontalScrollBar().addAdjustmentListener(scrolling);
		jsp.getVerticalScrollBar().addAdjustmentListener(scrolling);
		
		this.add(jsp, BorderLayout.SOUTH);
		this.add(outils, BorderLayout.NORTH);
		
		this.setJMenuBar(jmb);
		this.pack();
		this.setVisible(true);
	}
	
	// Pour capter quand on scroll la toile
	public class ScrollListener implements AdjustmentListener {
	    public void adjustmentValueChanged(AdjustmentEvent evt) {
	        if (evt.getValueIsAdjusting()) {
	        	VueFenetre.this.pack();
	        	VueFenetre.this.repaint();
	        }
	    }
	}

	
	public void setUndoBtnEnabled(boolean b) {
		this.undo.setEnabled(b);
		this.cancel.setEnabled(b);
	}
	
	public void setRedoBtnEnabled(boolean b) {
		this.redo.setEnabled(b);
		this.back.setEnabled(b);
	}
	
	public void setSauvegarderBtnEnabled(boolean b) {
		this.sauvegarder.setEnabled(b);
		this.save.setEnabled(b);
	}
	
	public void setCopyAndCutBtnEnabled(boolean b) {
		this.copier.setEnabled(b);
		this.copy.setEnabled(b);
		this.couper.setEnabled(b);
		this.cut.setEnabled(b);
	}
		
	public void setPasteBtnEnabled(boolean b) {
		this.coller.setEnabled(b);
		this.paste.setEnabled(b);
	}
	
	public void setGenerateBtnEnabled(boolean b) {
		this.generate.setEnabled(b);
		this.generer.setEnabled(b);
	}
	
	public void setLastFile(String tab[]) {
		if(tab.length > 0) {
			this.lastFiles.removeAll();
			
			for(int i = tab.length-1 ; i >= 0 ; i--) {
				if(tab[i] != null) {
					JMenuItem temp = new JMenuItem(tab[i]);
					temp.setActionCommand("OuvrirDerniers");
					temp.addActionListener(controleur);
					this.lastFiles.add(temp);
				}
			}
		}
			
	}
}
